<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="name" label="拼团名称">
          <el-input
            v-model="queryParams.name"
            clearable
            placeholder="请输入拼团名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="活动时间">
          <div class="block">
            <el-date-picker
              v-model="activityTimes"
              type="datetimerange"
              range-separator="至"
              start-placeholder="请选择"
              end-placeholder="请选择"
              @change="chooseActivityTimes"
            />
          </div>
        </el-form-item>

        <el-form-item prop="status" label="拼团状态">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择"
            @change="handleQuery"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card>
      <template #header>
        <el-button
          v-hasPerm="['act:festival:add']"
          type="success"
          @click="openAddDialog()"
        >
          <i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        v-loading="loading"
        highlight-current-row
        border
        :data="groupActivityList"
      >
        <el-table-column label="ID" align="center" prop="id" min-width="5%" />
        <el-table-column
          label="活动名称"
          align="center"
          prop="name"
          min-width="20%"
        />
        <el-table-column label="活动时间" align="center" min-width="25%">
          <template #default="scope">
            {{ processingTime(scope.row.startTime, scope.row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="待成团"
          align="center"
          prop="numberOfGroupsWaiting"
          min-width="10%"
        />
        <el-table-column
          label="已成团"
          align="center"
          prop="numberOfGroupsSuccess"
          min-width="10%"
        />
        <el-table-column label="状态" align="center" min-width="10%">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="success">未开始</el-tag>
            <el-tag v-if="scope.row.status === 1" type="success">进行中</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success">已暂停</el-tag>
            <el-tag v-if="scope.row.status === 3" type="success">已结束</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          min-width="20%"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              size="small"
              @click="openEditDialog(row.id, true)"
              ><i-ep-Collection />查看</el-button
            >
            <el-button
              v-if="row.status !== 3"
              v-hasPerm="['act:festival:edit']"
              type="primary"
              link
              size="small"
              @click="openEditDialog(row.id, false)"
              ><i-ep-edit />编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <addEdit ref="addEditDialogRef" @confirm="handleQuery" />
  </div>
</template>

<script setup lang="ts">
import addEdit from "@/views/marketing/group/add-edit.vue";
import { GroupActivityPageVO, GroupActivityQuery } from "@/api/group/types";
import { getGroupActivityPage } from "@/api/group/index";
import { formatDate } from "@/utils/dateUtils";

const queryFormRef = ref(ElForm);
const addEditDialogRef = ref();
let groupActivityList = ref<GroupActivityPageVO[]>([]);
let loading = ref<boolean>(false);
let total = ref(0);
const queryParams = reactive<GroupActivityQuery>({
  pageNum: 1,
  pageSize: 10,
  name: "",
  status: undefined,
  startTime: "",
  endTime: "",
});

let activityTimes = ref<[Date | undefined, Date | undefined]>();
function handleQuery() {
  loading.value = true;
  getGroupActivityPage(queryParams)
    .then(({ data }) => {
      groupActivityList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
function processingTime(startTime: string, endTime: string) {
  return startTime + " - " + endTime;
}

function openAddDialog() {
  addEditDialogRef.value.openAddDialog();
}

function openEditDialog(id: number, isDisable: boolean) {
  addEditDialogRef.value.openEditDialog(id, isDisable);
}

//这里正常要取数据字典的数据, 但后端没提供接口
//0 未开始， 1进行中， 2 已暂停， 3 已结束
const statusOptions: OptionType[] = [
  { label: "未开始", value: 0 },
  { label: "进行中", value: 1 },
  { label: "已暂停", value: 2 },
  { label: "已结束", value: 3 },
];

function chooseActivityTimes() {
  //赋值时间给 query
  queryParams.startTime = formatDate(
    activityTimes.value?.[0],
    "YYYY-mm-dd HH:MM:SS"
  );

  queryParams.endTime = formatDate(
    activityTimes.value?.[1],
    "YYYY-mm-dd HH:MM:SS"
  );

  handleQuery();
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  activityTimes.value = [undefined, undefined];
  handleQuery();
}

onMounted(() => {
  handleQuery();
});
</script>
